#!/usr/bin/python  
#coding=utf-8  
   
'''
@ info: Log Base Class
@ date: 2012-03-06
@ author: huangzy
'''

import logging,os
from django.conf import settings
from datetime import datetime
from random import random

class Log:
    def __init__ (self,path=None,level='DEBUG',formatters="[%(asctime)s][%(levelname)8s] - %(message)s"):
        if path==None:
            dirpath = '%s/static/log/%s/%s'%(settings.WEB_ROOT,datetime.now().strftime('%Y'),datetime.now().strftime('%m'))
            path = '%s/%s.log'%(dirpath,datetime.now().strftime('%Y%m%d'))
            newp = os.path.join(dirpath)
            # Make sure upload_path exists.
            if not os.path.exists(newp):
                os.makedirs(newp)
        self.logger = logging.getLogger("aaa")
        self.path = path
        self.level = eval('logging.'+level.upper())
        self.formatters = formatters
        self.init(self.path,self.level,self.formatters)

    def init(self,path,level,form):
        self.logger.setLevel(level)
        fh = logging.FileHandler(path)
        fh.setLevel(level)
        formatter = logging.Formatter(self.formatters)
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)
    
    def debug(self,msg):
        return self.logger.debug(msg)
        
    def info(self,msg):
        return self.logger.info(msg)

    def warn(self,msg):
        return self.logger.warn(msg)

    def error(self,msg):
        return self.logger.error(msg)

    def critical(self,msg):
        return self.logger.critical(msg)
    
    def begin(self,msg=None):
        if msg==None:
            msg = 'Begin'
        self.info('------------- %s -----------\n\n'%msg)
        

if __name__=='__main__':
    print '''Log Class mod'''


